﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace FT.Architecture.Queries
{
    public class SortClause<T>
    {
        private readonly Query<T> query;
        private readonly IList<SortClauseItem<T>> items;

        public SortClause(Query<T> query)
        {
            this.query = query;
            items = new List<SortClauseItem<T>>();
            Clear();
        }

        public IList<SortClauseItem<T>> Items
        {
            get
            {
                return items;
            }
        }

        public SortClauseItem<T> By(Expression<Func<T, object>> sortMember)
        {
            var item = new SortClauseItem<T>(sortMember, this);
            items.Add(item);
            return item;
        }

        public SortClause<T> By(Expression<Func<T, object>> sortMember, SortOrder sortOrder)
        {
            var item = new SortClauseItem<T>(sortMember, sortOrder, this);
            items.Add(item);
            return this;
        }

        public SortClauseItem<T> And(Expression<Func<T, object>> sortMember)
        {
            return By(sortMember);
        }

        public SortClause<T> And(Expression<Func<T, object>> sortMember, SortOrder sortOrder)
        {
            return By(sortMember, sortOrder);
        }

        public SortClause<T> Clear()
        {
            items.Clear();
            return this;
        }

        public Query<T> Done()
        {
            return query;
        }
    }
}